package com.anjubao.parking.municipal.user.controller;

import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.common.exception.ServiceException;
import com.anjubao.parking.municipal.user.dto.*;
import com.anjubao.parking.municipal.user.feign.UserFeignService;
import com.anjubao.parking.municipal.user.form.UserBalancePayForm;
import com.anjubao.parking.municipal.user.form.UserInfoUpdateForm;
import com.anjubao.parking.municipal.user.form.VehicleBindForm;
import com.anjubao.parking.municipal.user.service.UserBalanceRecordService;
import com.anjubao.parking.municipal.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会员-小程序
 *
 * @author zhuchunfeng
 * @date 2019/6/19 11:06
 */
@RestController
@RequestMapping("/parking-app-user")
@Slf4j
@Api(value = "user-service", tags = {"会员小程序"})
public class ParkingUserAPPController implements UserFeignService {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserBalanceRecordService userBalanceRecordService;

    @ApiOperation("2.1.1 获取验证码接口")
    @GetMapping("/captcha/{phone}")
    public ApiResult<CaptchaDto> getCaptcha(@ApiParam(value = "手机号",required = true) @PathVariable(value = "phone",required = true) String phone) {
        if (!phone.matches("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,3-9]))\\d{8}$")) {
            throw new ServiceException("无效的手机号");
        }
        CaptchaDto captcha = userInfoService.getCaptcha(phone);
        return ApiResult.success(captcha);
    }

    @ApiOperation("2.1.2 校验验证码接口")
    @PostMapping("/captcha/validate")
    public ApiResult<UserInfoDto> validateCaptcha(@ApiParam(value = "手机号", required = true) @RequestParam(value = "phone", required = true) String phone,
                                                  @ApiParam(value = "验证码", required = true) @RequestParam(value = "captcha", required = true) String captcha) {
        UserInfoDto userAccountDto = userInfoService.validateCaptcha(phone, captcha);
        return ApiResult.success(userAccountDto);
    }

    /**
     * 若用户不存在返回null
     */
    @ApiOperation("2.1.3 获取用户信息")
    @GetMapping("/user")
    public ApiResult<UserDetailDto> getUserInfo(@ApiParam(value = "32位用户编号", required = true) @RequestParam(value = "userCode", required = true) String userCode) {
        return ApiResult.success(userInfoService.getUserDetailByUserCode(userCode));

    }

    /**
     * 通过userCode修改用户信息
     * 若用户不存在抛出异常
     */
    @ApiOperation("2.1.3_1 通过userCode修改用户信息")
    @PostMapping("/user/user-code")
    public ApiResult<UserDetailDto> updateUserInfoByUserCode(@RequestBody UserInfoUpdateForm userInfoUpdateForm) {
        return ApiResult.success(userInfoService.updateUserInfoByUserCode(userInfoUpdateForm));
    }

    /**
     * 通过unionID修改用户信息
     * 若用户不存在抛出异常
     */
    @ApiOperation("2.1.3_2 通过unionID修改用户信息")
    @PostMapping("/user/union-id")
    public ApiResult<UserDetailDto> updateUserInfoByUnionId(@RequestBody UserInfoUpdateForm userInfoUpdateForm){
        return ApiResult.success(userInfoService.updateUserInfoByUnionId(userInfoUpdateForm));
    }



    /**
     * 用户余额充值,余额不足抛出异常
     */
    @PostMapping("/users/balances")
    @ApiOperation("2.1.4 用户余额充值消费接口")
    public ApiResult<UserBalanceDto> balancePay(@RequestBody UserBalancePayForm userBalancePayForm) {
        userBalancePayForm.validate();
        return ApiResult.success(userInfoService.balancePay(userBalancePayForm));
    }

    /**
     * 用户不存在返回null
     */
    @GetMapping("/users/vehicles")
    @ApiOperation("2.1.6 用户已绑定车牌获取接口")
    public ApiResult<UserInfoDto> getUserVehicles(@ApiParam(value = "32位用户编号", required = true) @RequestParam(value = "userCode", required = true) String userCode) {
        return ApiResult.success(userInfoService.getUserVehicles(userCode));
    }

    /*
     * 查询指定月的充值消费记录
     */
    @GetMapping("/users/balances/records")
    @ApiOperation("2.1.7 用户充值消费账单查询接口")
    public ApiResult<List<UserBalanceRecordDto>> getUserBalanceRecords(@ApiParam(value = "16位用户编号", required = true) @RequestParam(value = "userCode", required = true) String userCode,
                                                                                   @ApiParam(value = "1充值 2消费 不传查全部",required = false) @RequestParam(value = "type",required = false) Integer type,
                                                                                   @ApiParam(value = "当前页码，默认为第一页") @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                       @ApiParam(value = "分页大小，默认分页大小为10") @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {

        IPage<UserBalanceRecordDto> userBalanceRecords = userBalanceRecordService.getUserBalanceRecords(userCode, type, page, pageSize);
        return ApiResult.success(userBalanceRecords.getRecords(),(int)userBalanceRecords.getTotal());
    }

    /**
     * 通过车牌号查询用户信息,用户不存在返回null
     */
    @GetMapping("/vehicle/user")
    @ApiOperation("2.1.8 通过车牌号获取用户")
    public ApiResult<UserInfoDto> getUserByVehicle(@ApiParam(value = "车牌号", required = true) @RequestParam(value = "vehicle", required = true) String vehicle) {
        return ApiResult.success(userInfoService.getUserByVehicle(vehicle));
    }

    @PostMapping("/vehicle/bind")
    @ApiOperation("2.1.9 解绑/绑定 车牌号")
    public ApiResult<UserInfoDto> bindVehicle(@RequestBody VehicleBindForm vehicleBind) {
        return ApiResult.success(userInfoService.bindVehicle(vehicleBind));
    }

}
